package com.soft.servlet;

import com.soft.entity.Goods;
import com.soft.service.Domain;
import com.soft.util.PropUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@WebServlet(name = "GoodsUpdateServlet",urlPatterns = "/GoodsUpdate")
public class GoodsUpdateServlet extends HttpServlet {

    private Domain domain;
    private Boolean isMultipart;
    //private FileItem fileItem;//暂时存储文件
    private int maxFileSize = 1024 * 1024 * 10;
    private int maxMemSize = 100 * 1024;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        domain = new Domain();
        int goodsId = Integer.parseInt(req.getParameter("goodsId"));
        Goods goods = domain.getGoodsById(goodsId);
        if (goods != null) {
            req.setAttribute("Goods", goods);
        } else {
            req.getSession().setAttribute("flag", 28);
        }
        req.getRequestDispatcher("/toGoodsManageEdit.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        domain = new Domain();

        String id = null;
        String name = null;
        String price = null;
        //String date = null;
        String count = null;
        //String type = null;
        String detail = null;
        Goods goods = new Goods();

        isMultipart = ServletFileUpload.isMultipartContent(req);

        if (!isMultipart) {
            return;
        }

        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(maxMemSize);

        String path = PropUtil.getProperty("img.path");
        String path1 = PropUtil.getProperty("img.path1");
        factory.setRepository(new File(path));

        // 创建一个新的文件上传处理程序
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setSizeMax(maxFileSize);

        try {
            // 解析请求，获取文件项
            List fileItems = upload.parseRequest(req);
            // 处理上传的文件项
            Iterator i = fileItems.iterator();
            //Iterator i1 = fileItems.iterator();
            while (i.hasNext()) {
                FileItem fi = (FileItem) i.next();
                //FileItem fi1 = (FileItem) i1.next();
                if (!fi.isFormField()) {
                    //File file = new File(path + "goods" + id + ".jpg");
                    File file1 = new File(path1 + "goods" + id + ".jpg");
                    //if (file.exists()) file.delete();
                    if (file1.exists()) file1.delete();
                    fi.write(file1);
                    //fi1.write(file1);
                } else {
                    String value = fi.getString("utf-8");
                    if (fi.getFieldName().equals("shopGoods.id")) id = value;
                    else if (fi.getFieldName().equals("shopGoods.goodsname")) name = value;
                    else if (fi.getFieldName().equals("shopGoods.price")) price = value;
                    else if (fi.getFieldName().equals("shopGoods.count")) count = value;
                    //else if (fi.getFieldName().equals("shopGoods.type")) type= value;
                    else if (fi.getFieldName().equals("shopGoods.introduce")) detail = value;
                }
            }
            goods = domain.getGoodsById(Integer.parseInt(id));
            goods.setName(name);
            goods.setPrice(price);
            //goods.setCreateTime(date);
            goods.setCount(count);
            //goods.setType(type);
            goods.setDetail(detail);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (domain.updateGoods(goods)) {
            req.getSession().setAttribute("flag", 29);
            req.getRequestDispatcher("/GoodsList").forward(req, resp);
        } else {
            req.getSession().setAttribute("flag", 32);
            doGet(req, resp);
        }
    }
}
